﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jayden.Dll.Core
{
	public struct Time
	{
		private int? m_Hour;
		private int? m_Minute;
		private int? m_Second;
		private int? m_Millisecond;

		public int? Hour { get { return m_Hour; } set { m_Hour = value; Validate(); } }
		public int? Minute { get { return m_Minute; } set { m_Minute = value; Validate(); } }
		public int? Second { get { return m_Second; } set { m_Second = value; Validate(); } }
		public int? Millisecond { get { return m_Millisecond; } set { m_Millisecond = value; Validate(); } }

		public Time(System.DateTime dateTime)
		{
			m_Hour = dateTime.Hour;
			m_Minute = dateTime.Minute;
			m_Second = dateTime.Second;
			m_Millisecond = dateTime.Millisecond;
		}
		public Time(int? hour, int? minute, int? second, int? millisecond)
		{
			m_Hour = hour;
			m_Minute = minute;
			m_Second = second;
			m_Millisecond = millisecond;
		}

		public bool HasHour { get { return m_Hour.HasValue; } }
		public bool HasMinute { get { return m_Minute.HasValue; } }
		public bool HasSecond { get { return m_Second.HasValue; } }
		public bool HasMillisecond { get { return m_Millisecond.HasValue; } }

		public bool HasAll { get { return HasHour && HasMinute && HasSecond && HasMillisecond; } }

		private void Validate()
		{
			if (HasHour)
				if (Hour.Value < 0 || Hour.Value > 23)
					throw new Exception("Invalid hour value.");
			if (HasMinute)
				if (Minute.Value < 0 || Minute.Value > 59)
					throw new Exception("Invalid minute value.");
			if (HasSecond)
				if (Second.Value < 0 || Second.Value > 59)
					throw new Exception("Invalid second value.");
			if (HasMillisecond)
				if (Millisecond.Value < 0 || Millisecond.Value > 999)
					throw new Exception("Invalid millisecond value.");
		}
	}
}
